//
//  UIViewController+ImagePicker.m
//  JunYouLawer
//
//  Created by Apple on 16/9/5.
//  Copyright © 2016年 junyou. All rights reserved.
//

#import "UIViewController+ImagePicker.h"
#import "JUAlertView.h"
#import "objc/runtime.h"
#import <AssetsLibrary/ALAssetsLibrary.h>
#import "UIImage+Color.h"
#import "UIImage+Convert.h"
static  BOOL canEdit = NO;
static  char blockKey;

@interface UIViewController()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic,copy)imageBlock imageBlock;

@end

@implementation UIViewController (ImagePicker)



#pragma mark-set
- (void)setImageBlock:(imageBlock)imageBlock
{
    objc_setAssociatedObject(self, &blockKey, imageBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark-get
- (imageBlock )imageBlock{
    return objc_getAssociatedObject(self, &blockKey);
}

- (void)showCanEdit:(BOOL)edit image:(imageBlock)block
{
    if (edit) {
        canEdit = edit;
    }
    
    self.imageBlock = [block copy];
    
    [JUAlertView showActionSheetWith:self title:nil message:nil callbackBlock:^(NSInteger btnIndex) {
        ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied) {
            NSString *title = nil;
            NSString *photoType = btnIndex == 0 ? @"相机":@"相册";
            NSString *msg = [NSString stringWithFormat:@"还没有开启%@权限,请在系统设置中开启",photoType];
            NSString *cancelTitle = @"暂不";
            NSString *otherButtonTitle = @"去设置";
            
            if (iOS_Version < 8.0) {
                title = [NSString stringWithFormat:@"%@权限未开启",photoType];
                msg = [NSString stringWithFormat:@"请在系统设置中开启%@服务\n(设置>隐私>%@>开启)",photoType,photoType];
                cancelTitle = @"知道了";
                otherButtonTitle = nil;
            }
            [JUAlertView showAlertWith:self title:title message:msg callbackBlock:^(NSInteger btnIndex) {
                if (btnIndex == 1) {
                        if (iOS_Version >= 8.0) {
                        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                        if ([[UIApplication sharedApplication] canOpenURL:url]) {
                            [[UIApplication sharedApplication] openURL:url];
                        }
                    }
                }
                
            } cancelButtonTitle:cancelTitle destructiveButtonTitle:nil otherButtonTitles:otherButtonTitle, nil];
            
            
            return;
        }
        //跳转到相机/相册页面
        UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = canEdit;
        switch (btnIndex)
        {
            case 1:
                //拍照
                //是否支持相机
                if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                {
                    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:imagePickerController animated:YES completion:NULL];
                }
                else
                {
                    [JUAlertView showTipAlertViewWith:self title:@"温馨提示" message:@"该设备不支持相机" buttonTitle:@"确定" buttonStyle:JXTAlertActionStyleDefault];
                }
                break;
            case 2:
                //相册
                imagePickerController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:imagePickerController animated:YES completion:NULL];
            default:
                break;
        }
        
    } destructiveButtonTitle:nil cancelButtonTitle:@"取消" otherButtonTitles:@"相机",@"相册", nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image;
    //是否要裁剪
    if ([picker allowsEditing]){
        //编辑之后的图像
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    NSData *data;
    
    if (UIImagePNGRepresentation(image) == nil) {
        
        data = UIImageJPEGRepresentation(image, 1);
        
    } else {
        
        data = UIImagePNGRepresentation(image);
        
    }
    
    if(self.imageBlock)
    {
        NSLog(@"%@",info);
        self.imageBlock(image);
    }
}

@end
